﻿using System;
using System.Configuration;
using System.Data.SqlClient;
using System.Web.UI.WebControls;

namespace wall
{
    public partial class MessageBoard : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadMessages();
            }
        }

        private void LoadMessages()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;

            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                string query = "SELECT Id, Username, Message, DatePosted, AdminReply FROM Messages WHERE IsDeleted = 0";
                SqlCommand command = new SqlCommand(query, connection);

                try
                {
                    connection.Open();
                    SqlDataAdapter adapter = new SqlDataAdapter(command);
                    System.Data.DataTable dt = new System.Data.DataTable();
                    adapter.Fill(dt);
                    gvMessages.DataSource = dt;
                    gvMessages.DataBind();
                }
                catch (Exception ex)
                {
                    Response.Write("发生错误：" + ex.Message);
                }
            }
        }
    }
}